package com.gitee.hljdrl.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.LayoutRes;

import com.gitee.hljdrl.dialog.basis.POPDialogEvent;
import com.gitee.hljdrl.dialog.basis.ViewPartVisitor;

/**
 * PopupWindow
 */
public class UIPOPDialog extends PopupWindow implements POPDialogEvent {

    protected int mLayoutId = 0;

    protected ViewPartVisitor mViewPartVisitor;
    //------------------------------------------
    private View mView;
    //------------------------------------------
    protected Activity mActivity;
    protected boolean enableDim = false;
    public UIPOPDialog(Activity activity) {
        this.mActivity = activity;
    }
    public UIPOPDialog(Activity activity, int animationStyle) {
        this.mActivity = activity;
        setAnimationStyle(animationStyle);
    }

    public UIPOPDialog(Activity activity, int animationStyle, int width, int height) {
        this.mActivity = activity;
        setAnimationStyle(animationStyle);
        setWidth(width);
        setHeight(height);
    }

    @Override
    public UIPOPDialog setLayoutId(@LayoutRes int layoutId) {
        mLayoutId = layoutId;
        return this;
    }

    public void setEnableDim(boolean enableDim) {
        this.enableDim = enableDim;
    }

    @Override
    public Context getContext() {
        return mActivity;
    }

    @Override
    public UIPOPDialog setViewPartVisitor(ViewPartVisitor viewPartVisitor) {
        mViewPartVisitor = viewPartVisitor;
        onCreate();
        return this;
    }

    @Override
    public View getView() {
        return mView;
    }

    protected void onCreate() {
        setFocusable(true);
        setOutsideTouchable(true);

        int layout = mLayoutId;
        if (mViewPartVisitor != null && mViewPartVisitor.getLayout() != 0) {
            layout = mViewPartVisitor.getLayout();
        }
        View contentView = View.inflate(mActivity, layout, null);
        mView = contentView;
        setContentView(mView);
        initView(contentView);
    }


    protected void initView(View contentView) {
        if (mViewPartVisitor != null) {
            try {
                mViewPartVisitor.onViewVisit(this, contentView);
            } catch (Exception ex) {
                //TODO
            }
        }
    }

    private int getGravity(){
        if(mViewPartVisitor!=null){
            return mViewPartVisitor.getGravity();
        }
        return Gravity.CENTER;
    }

    public void dismiss() {
        if(isShowing()){
            setWindowBackgroundDim(mActivity,false);
        }
        super.dismiss();
    }



    @Override
    public void show(Activity activity) {
        show(activity.getWindow().getDecorView());
    }

    @Override
    public void show(View view) {
        show(view, 0, 0);
    }

    @Override
    public void show(View view, int x, int y) {
        final int gravity = getGravity();
        switch (gravity) {
            case Gravity.CENTER:
                showAtLocation(view, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, x, y);
                break;
            case Gravity.TOP:
                showAtLocation(view, Gravity.TOP | Gravity.CENTER_HORIZONTAL, x, y);
                break;
            case Gravity.BOTTOM:
                showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, x, y);
                break;
        }
        setWindowBackgroundDim(mActivity,true);
    }

    // 设置背景的透明度
    private void setWindowBackgroundDim(Activity activity, boolean dim) {
        if(!enableDim){
            return;
        }
        Window window = activity.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        if (dim) {
            layoutParams.alpha = 0.5f;  // 设置半透明（0.0f 表示完全透明，1.0f 表示完全不透明）
        } else {
            layoutParams.alpha = 1.0f;  // 恢复为不透明
        }
        window.setAttributes(layoutParams);
    }
}
